package com.meng.hooklogin;

import java.lang.reflect.Field;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * Created by Administrator on 2017/3/9.
 */

public class Main implements IXposedHookLoadPackage{
    //    @Override
//    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
//        if(!loadPackageParam.packageName.equals("com.meng.login_demo")){
//            return;
//        }
//        XposedBridge.log("Loaded app: "+ loadPackageParam.packageName);
//
//        //Hook com.meng.login_demo.MainActivity中的isCrrectInfo(String,String)方法
//        XposedHelpers.findAndHookMethod("com.meng.login_demo.MainActivity", loadPackageParam.classLoader, "isCorrectInfo"
//                , String.class, String.class, new XC_MethodHook() {
//                    @Override
//                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                        XposedBridge.log("开始劫持了~");
//                        XposedBridge.log("参数1 = " + param.args[0]);
//                        XposedBridge.log("参数2 = " + param.args[1]);
//                        param.args[0] = "mengmenglove";
//                    }
//
//                    @Override
//                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                        XposedBridge.log("劫持结束了~");
//                        XposedBridge.log("参数1 = " + param.args[0]);
//                        XposedBridge.log("参数2 = " + param.args[1]);
//                    }
//                });
//    }
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {


        if(loadPackageParam.packageName.equals("com.meng.login_demo")){
            XposedBridge.log("*************Loaded app: "+ loadPackageParam.packageName);
            XposedHelpers.findAndHookMethod("com.meng.login_demo.MainActivity", loadPackageParam.classLoader, "isCorrectInfo"
                    , String.class, String.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("开始劫持了~");
                            XposedBridge.log("参数1 = " + param.args[0]);
                            XposedBridge.log("参数2 = " + param.args[1]);
                            param.args[0] = "mengmenglove";
                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("劫持结束了~");
                            XposedBridge.log("参数1 = " + param.args[0]);
                            XposedBridge.log("参数2 = " + param.args[1]);
                        }
                    });
            XposedHelpers.findAndHookMethod("com.meng.login_demo.Meng", loadPackageParam.classLoader, "Meng"
                    , String.class, String.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("开始劫持了~");
                            XposedBridge.log("参数1 = " + param.args[0]);
                            param.args[0] = "mengmenglove!!!!!!!!!!!!!";
                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("劫持结束了~");
                            XposedBridge.log("参数1 = " + param.args[0]);
                        }
                    });


        }else if(loadPackageParam.packageName.equals("me.ele")){
            XposedBridge.log("*************Loaded app: "+ loadPackageParam.packageName);
//Hook com.meng.login_demo.MainActivity中的isCrrectInfo(String,String)方法
            final ClassLoader classLoader = loadPackageParam.classLoader;
            Class<?> loadClass = classLoader.loadClass("com.tencent.mm.sdk.modelbase.BaseReq");//
            XposedHelpers.findAndHookMethod("com.tencent.mm.sdk.openapi.WXApiImplV10", classLoader, "sendReq"
                    , loadClass, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("开始劫持了~");
                            Object reqObj = param.args[0];
                            XposedBridge.log("参数1 = " + reqObj);
                            Class<?> reqClass = classLoader.loadClass("com.tencent.mm.sdk.modelmsg.SendMessageToWX$Req");
                            XposedBridge.log("---reqClass:"+reqClass);
                            Field messageField = reqClass.getField("message");//
                            XposedBridge.log("---messageField:"+messageField);
                            Object messageObj = messageField.get(reqObj);
                            XposedBridge.log("---messageObj:"+messageObj);
                            Class<?> WXMediaMessageClass = classLoader.loadClass("com.tencent.mm.sdk.modelmsg.WXMediaMessage");//
                            XposedBridge.log("---WXMediaMessageClass:"+WXMediaMessageClass);
                            Field mediaObjectField = WXMediaMessageClass.getField("mediaObject");//
                            XposedBridge.log("---mediaObjectField:"+mediaObjectField);
                            Object mediaObject = mediaObjectField.get(messageObj);
                            XposedBridge.log("---mediaObject:"+mediaObject);
                            Class<?> WXWebpageObjectClass = classLoader.loadClass("com.tencent.mm.sdk.modelmsg.WXWebpageObject");//
                            XposedBridge.log("---WXWebpageObjectClass:"+WXWebpageObjectClass);
                            Field webpageUrlField = WXWebpageObjectClass.getField("webpageUrl");//
                            XposedBridge.log("---webpageUrlField:"+webpageUrlField);
                            Object webpageUrl = webpageUrlField.get(mediaObject);
                            XposedBridge.log("---webpageUrl:"+webpageUrl);
                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("劫持结束了~");
                        }
                    });
        }else if(loadPackageParam.packageName.equals("com.meng.umengshared_demo")){
            XposedBridge.log("*************Loaded app: "+ loadPackageParam.packageName);

            //Hook com.meng.login_demo.MainActivity中的isCrrectInfo(String,String)方法
            final ClassLoader classLoader = loadPackageParam.classLoader;
            Class<?> loadClass = classLoader.loadClass("com.tencent.mm.sdk.openapi.BaseReq");
            XposedHelpers.findAndHookMethod("com.tencent.mm.sdk.openapi.WXApiImplV10", classLoader, "sendReq"
                    , loadClass, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("开始劫持了~");
                            Object reqObj = param.args[0];
                            XposedBridge.log("参数1 = " + reqObj);
                            Class<?> reqClass = classLoader.loadClass("com.tencent.mm.sdk.openapi.SendMessageToWX$Req");
                            XposedBridge.log("---reqClass:"+reqClass);
                            Field messageField = reqClass.getField("message");
                            XposedBridge.log("---messageField:"+messageField);
                            Object messageObj = messageField.get(reqObj);
                            XposedBridge.log("---messageObj:"+messageObj);
                            Class<?> WXMediaMessageClass = classLoader.loadClass("com.tencent.mm.sdk.openapi.WXMediaMessage");
                            XposedBridge.log("---WXMediaMessageClass:"+WXMediaMessageClass);
                            Field mediaObjectField = WXMediaMessageClass.getField("mediaObject");
                            XposedBridge.log("---mediaObjectField:"+mediaObjectField);
                            Object mediaObject = mediaObjectField.get(messageObj);
                            XposedBridge.log("---mediaObject:"+mediaObject);
                            Class<?> WXWebpageObjectClass = classLoader.loadClass("com.tencent.mm.sdk.openapi.WXWebpageObject");
                            XposedBridge.log("---WXWebpageObjectClass:"+WXWebpageObjectClass);
                            Field webpageUrlField = WXWebpageObjectClass.getField("webpageUrl");
                            XposedBridge.log("---webpageUrlField:"+webpageUrlField);
                            Object webpageUrl = webpageUrlField.get(mediaObject);
                            XposedBridge.log("---webpageUrl:"+webpageUrl);
                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("劫持结束了~");
                        }
                    });
        }else if(loadPackageParam.packageName.equals("com.mgxiaoyuan.flower")){ //有模有样
            XposedBridge.log("****start Loaded app: "+ loadPackageParam.packageName);
//Hook com.meng.login_demo.MainActivity中的isCrrectInfo(String,String)方法
            final ClassLoader classLoader = loadPackageParam.classLoader;
//            Class<?> loadClass = classLoader.loadClass("com.tencent.mm.sdk.modelbase.BaseReq");//

            XposedHelpers.findAndHookMethod("retrofit2.ServiceMethod.Builder", classLoader, "parseHttpMethodAndPath"
                    , String.class,String.class,boolean.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("开始劫持了~");
                            Object reqObj1 = param.args[0];
                            XposedBridge.log("参数1 = " + reqObj1);
                            Object reqObj2 = param.args[1];
                            XposedBridge.log("参数2 = " + reqObj2);
                            //将https替换成http
                            param.args[1] = param.args[1].toString().replace("https","http");
                            Object reqObj3 = param.args[2];
                            XposedBridge.log("参数3 = " + reqObj3);


                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("劫持后的参数~");
                            Object reqObj2 = param.args[1];
                            XposedBridge.log("参数2 = " + reqObj2);
                            XposedBridge.log("劫持结束了~");

                        }
                    });
            XposedBridge.log("****end Loaded app: "+ loadPackageParam.packageName);
        }else if(loadPackageParam.packageName.equals("com.lg.youmoyouyang")){ //有模有样
            XposedBridge.log("****start Loaded app: "+ loadPackageParam.packageName);
//Hook com.meng.login_demo.MainActivity中的isCrrectInfo(String,String)方法
            final ClassLoader classLoader = loadPackageParam.classLoader;
//            Class<?> loadClass = classLoader.loadClass("com.tencent.mm.sdk.modelbase.BaseReq");//
            XposedHelpers.findAndHookMethod("retrofit2.ServiceMethod.Builder", classLoader, "parseHttpMethodAndPath"
                    , String.class,String.class,boolean.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("开始劫持了~");
                            Object reqObj1 = param.args[0];
                            XposedBridge.log("参数1 = " + reqObj1);
                            Object reqObj2 = param.args[1];
                            XposedBridge.log("参数2 = " + reqObj2);
                            Object reqObj3 = param.args[2];
                            XposedBridge.log("参数3 = " + reqObj3);


                        }
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            XposedBridge.log("劫持结束了~");
                        }
                    });
            XposedBridge.log("****end Loaded app: "+ loadPackageParam.packageName);
        }
//        else if(loadPackageParam.packageName.equals("com.alibaba.android.rimet")){//钉钉打卡
//            XposedBridge.log("*************Loaded app: "+ loadPackageParam.packageName);
//
//            //Hook com.meng.login_demo.MainActivity中的isCrrectInfo(String,String)方法
//            final ClassLoader classLoader = loadPackageParam.classLoader;
//            XposedHelpers.findAndHookMethod("com.amap.api.location.AMapLocation", classLoader, "getAddress"
//                    , new XC_MethodHook() {
//                        @Override
//                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                            XposedBridge.log("getAddress~~~开始劫持了~");
//                            Object object = param.getResult();
//                            XposedBridge.log("劫持前地址："+object);
//                            param.setResult("四川省成都市锦江区合江亭街道下东大街段花样年·喜年广场");
//                        }
//                        @Override
//                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                            XposedBridge.log("劫持结束了~");
//                            XposedBridge.log("劫持后地址："+param.getResult());
//                        }
//                    });
//        }
    }
}
